package com.wkbb.basic.thread;

import com.wkbb.basic.feign.TcCloudFeignService;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.im.GroupUserInfoDto;
import com.wkbb.common.redis.RedisTemplateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author dengjingyuan
 * @date 2020-08-23 13:20
 */
@Slf4j
public class ImRepairFamilyExcleTask implements Runnable {


    private TcCloudFeignService tcCloudFeignService;

    private List<List<GroupUserInfoDto>> groupUserInfoDtoList;

    private Long orgId;

    private RedisTemplateUtil redisTemplateUtil;

    public ImRepairFamilyExcleTask(TcCloudFeignService tcCloudFeignService, List<List<GroupUserInfoDto>> groupUserInfoDtoList,Long orgId,RedisTemplateUtil redisTemplateUtil) {
        this.tcCloudFeignService = tcCloudFeignService;
        this.groupUserInfoDtoList = groupUserInfoDtoList;
        this.orgId = orgId;
        this.redisTemplateUtil = redisTemplateUtil;
    }

    public ImRepairFamilyExcleTask() {

    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            //所有家长注册完毕后才能入群
            Boolean flag=false;
            ResultDTO<String> bulkRegisterImAccountRedisLock = tcCloudFeignService.getBulkRegisterImAccountRedisLock(orgId);

            if(ResultDTO.checkSuccess(bulkRegisterImAccountRedisLock)&&null!=bulkRegisterImAccountRedisLock.getData()){
                Object result = redisTemplateUtil.get(bulkRegisterImAccountRedisLock.getData());
                if(null!=result){
                    flag = new Boolean(result.toString());
                    do {
                        flag = Boolean.parseBoolean(redisTemplateUtil.get(bulkRegisterImAccountRedisLock.getData()).toString());
                    } while (flag == false);
                }
                tcCloudFeignService.repairFamilyExcle(orgId,groupUserInfoDtoList);
            }
        } catch (Exception e) {
            log.error("*******ImRepairFamilyExcleTask**********", e);
        }
    }
}
